package com.example.koizzi.ad20180606_note;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements DelStuData {
    private Button btnInsertStudent;
    private ListView stuListView;
    private ArrayList<student> stuArrayListData = new ArrayList<>();
    private StuAdapter adapeter;
    private MyBroadcastReceiver myBroadcastReceiver;
    private Uri uri = Uri.parse(MyContentProvider.URI_STUDENT);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        InsertStuData();//跳转到插入学生数据页面
        QueryStuData();//查询数据库，取得数据放入ArrayList
        new MyOpenHelper(this, "stu.db", null, 1);
        RegisterBroadcast();//注册广播
        ChangeStuData();//点击ListView更改数据

    }

    /*点击ListView更改数据 --start--*/
    private void ChangeStuData() {
        adapeter = new StuAdapter(this, stuArrayListData, this);
        stuListView = findViewById(R.id.stulistview);
        stuListView.setAdapter(adapeter);
        stuListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                int id = stuArrayListData.get(i).getId();
                String name = stuArrayListData.get(i).getName();
                int age = stuArrayListData.get(i).getAge();
                Intent intent = new Intent(MainActivity.this, EditActivity.class);
                intent.putExtra(StudentTable.COLUMN_ID, id);
                intent.putExtra(StudentTable.COLUMN_NAME, name);
                intent.putExtra(StudentTable.COLUMN_AGE, age);
                startActivity(intent);
            }
        });
    }
    /*点击ListView更改数据 --start--*/

    /*注册，注销广播 --start--*/
    private void RegisterBroadcast() {
        myBroadcastReceiver = new MyBroadcastReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction("INSERT_FINISHED");
        filter.addAction("DELETE_FINISHED");
        filter.addAction("CHANGE_FINISHED");
        registerReceiver(myBroadcastReceiver, filter);
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();

        unregisterReceiver(myBroadcastReceiver);
    }

    /*注册，注销广播 --end--*/
    /*查询数据库，取得数据放入ArrayList --start--*/
    private void QueryStuData() {
        stuArrayListData.clear();
        Cursor cursor = getContentResolver().query(uri, null, null, null, null);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                int id = cursor.getInt(cursor.getColumnIndex(StudentTable.COLUMN_ID));
                String name = cursor.getString(cursor.getColumnIndex(StudentTable.COLUMN_NAME));
                int age = cursor.getInt(cursor.getColumnIndex(StudentTable.COLUMN_AGE));
                student s = new student(id, name, age);
                stuArrayListData.add(s);
            }
            cursor.close();
        }

    }
    /*查询数据库，取得数据放入ArrayList --end--*/

    /*跳转到插入学生数据页面 --start--*/
    private void InsertStuData() {
        btnInsertStudent = findViewById(R.id.btn_insert_student);
        btnInsertStudent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intentToInsert = new Intent(MainActivity.this, InsertActivity.class);
                startActivity(intentToInsert);
            }
        });
    }
    /*跳转到插入学生数据页面 --end--*/

    /*接口DelStuData,点击删除，发送广播 --start--*/
    @Override
    public void delStuData(int id) {
        String where = "id=" + id;
        getContentResolver().delete(uri, where, null);
        Intent intentDelStuData = new Intent();
        intentDelStuData.setAction("DELETE_FINISHED");
        sendBroadcast(intentDelStuData);
    }
    /*接口DelStuData,点击删除，发送广播 --end--*/

    /*新建一个广播接收类,收到广播后刷新ListView,SQL --start--*/
    class MyBroadcastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            QueryStuData();
            adapeter.notifyDataSetChanged();
        }
    }
    /*新建一个广播接收类,收到广播后刷新ListView,SQL  --end--*/
}
